package com.lhl.fw.db.service;

import com.lhl.fw.db.repository.BaseJpaRepository;
import com.querydsl.core.types.OrderSpecifier;
import com.querydsl.core.types.Predicate;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;

import java.io.Serializable;
import java.util.List;
import java.util.Optional;

/**
 * @className: BaseJpaService
 * @description: TODO 类描述
 * @author: king
 * @date: 2020-09-03 16:18
 **/
public interface BaseJpaService<T, ID extends Serializable, R extends BaseJpaRepository<T, ID>> {

    R getBaseJpaRepository();

    // ****************JpaRepository********************/
    Optional<List<T>> findAll();

    Optional<List<T>> findAll(Sort sort);

    Optional<List<T>> findAllById(Iterable<ID> ids);

    <S extends T> List<S> saveAll(Iterable<S> entities);

    void flush();

    <S extends T> S saveAndFlush(S entity);

    void deleteInBatch(Iterable<T> entities);

    void deleteAllInBatch();

    /**
     * @param: id 参数为主键
     * @description: 没有直接调用数据库获取对象,属于懒加载方法
     * @return: T
     * @author: king
     * @date: 2020-09-04 17:41
     */
    T getOne(ID id);

    <S extends T> List<S> findAll(Example<S> example);

    <S extends T> List<S> findAll(Example<S> example, Sort sort);

    /**
     * ************PagingAndSortingRepository*************************
     */
    Page<T> findAll(Pageable pageable);

    /**
     * ****************CrudRepository**********************************************
     */
    <S extends T> S save(S entity);

    Optional<T> findById(ID id);

    boolean existsById(ID id);

    long count();

    void deleteById(ID id);

    void delete(T entity);

    void deleteAll(Iterable<? extends T> entities);

    void deleteAll();

    /**
     * ***********自定义*************************
     */
    Page<T> findAll(Predicate predicate, Pageable pageable, OrderSpecifier<?>... orders);
}
